<template>
  <div class="list">
    <div class="item" v-for="(item,index) in goodlist" :key="item.id">
      <div class="item-l">
        <img class="item-img" :src="require('../assets'+item.src)" />
      </div>
      <div class="item-r">
        <div class="item-title">
          {{item.title}}
          <small>x {{item.num}}</small>
        </div>
        <div class="item-price">{{item.price|money}}</div>
        <div class="item-opt">
          <button @click="Delete(index)">删除</button>
        </div>
      </div>
    </div>
    <div class="item-total" v-if="sum>0?true:false">商品总价：{{sum|money}}</div>
    <div class="item-empty" v-else>购物车中暂无商品</div>
  </div>
</template>

<script>
	import {mapGetters,mapState,mapMutations} from 'vuex'
	export default {
		name:"ShopCart",
		computed:{
			// goodlist(){
			// 	console.log(this.$store.state.shopcart.list)
			// 	return this.$store.state.shopcart.list
			// },
			//获取购物车列表
			...mapState('shopcart',{'goodlist':'list'}),
			//获取购物车总价
			...mapGetters('shopcart',['sum']),
		},
		methods:{
			// Delete(index){
			// 	this.$store.commit('shopcart/Delete',index)
			// }
			//删除指定商品
			...mapMutations('shopcart',['Delete'])
		},
		filters:{
			//价格前缀
			money(value){
				return '￥'+value
			}
		}
	}
</script>

<style>
.item {
  border-bottom: 1px dashed #ccc;
  padding: 10px;
}
.item::after {
  content: "";
  display: block;
  clear: both;
}
.item-l {
  float: left;
  font-size: 0;
}
.item-r {
  float: left;
  padding-left: 20px;
}
.item-img {
  width: 100px;
  height: 100px;
  border: 1px solid #ccc;
}
.item-title {
  font-size: 14px;
  margin-top: 10px;
}
.item-title > small {
  color: #888;
  font-size: 12px;
}
.item-price {
  margin-top: 10px;
  color: #f00;
  font-size: 15px;
}
.item-opt {
  margin-top: 10px;
}
.item-opt button {
  border: 0;
  background: coral;
  color: #fff;
  padding: 4px 5px;
}
.item-total {
  margin: 10px;
  font-size: 15px;
}
.item-empty {
  text-align: center;
  margin-top: 20px;
  font-size: 15px;
}
</style>